package sample;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.DragEvent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.TransferMode;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import sample.util.Dialogutil;
import sample.util.ImportExecl;
import sample.util.Reflectutil;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class Controller {

    @FXML
    private MenuItem exitbtn;

    private static File chooseFile;
    private static Stage stage;
    private static Stage tableStage;
    private static TextField fileText;

    private static ObservableList<Person> pList = FXCollections.observableArrayList(


    );
    private final String[] rightColumns = {"不动产权证号", "权利人", "身份证号码", "不动产坐落", "不动产单元号", "权利类型", "权利性质", "面积(土地)","面积(房屋)", "用途(土地)","用途(房屋)", "使用期限",
            "共有情况","证书编号","登记原因","登记类型","登记时间","原不动产权利证书号(土地)","原不动产权利证书号(房屋)","原地号","系统顺序号"};

    @FXML
    protected void setExitbtn(ActionEvent event) {
        System.out.println("被点击了");
        System.exit(0);


    }

    @FXML
    protected void oldItem() throws IOException {
        stage = new Stage();
        Parent root = FXMLLoader.load(getClass().getResource("old.fxml"));
        Button chooseBtn = (Button) root.lookup("#choosebtn");
        fileText = (TextField) root.lookup("#fileinput");
        //鼠标拖拽到框上
        fileText.setOnDragOver(event -> {
            if(event.getGestureSource()!=fileText){
                event.acceptTransferModes(TransferMode.ANY);


            }
        });
        // 拖拽文件到放开
        fileText.setOnDragDropped(event -> {
            Dragboard dragboard=event.getDragboard();
            if(dragboard.hasFiles()){
                File file=dragboard.getFiles().get(0);
                if(file!=null){
                    fileText.setText(file.getAbsolutePath());
                }
            }
        });
        chooseBtn.setOnAction((e) -> {
            FileChooser fileChooser = new FileChooser();
            fileChooser.setTitle("选择文件");
            chooseFile = fileChooser.showOpenDialog(stage);
            if (chooseFile != null) {
                fileText.setText(chooseFile.getAbsolutePath());


            }
        });
        chooseBtn.setGraphic(new ImageView(new Image(getClass().getResourceAsStream("/sample/images/文件.png"))));
        Button showbtn= (Button) root.lookup("#showbtn");
        showbtn.setGraphic(new ImageView(new Image(getClass().getResourceAsStream("images/查看.png"))));
        Button importbtn= (Button) root.lookup("#importbtn");
        importbtn.setGraphic(new ImageView(new Image(getClass().getResourceAsStream("images/导入.png"))));
        importbtn.setOnAction(e->{

        });
        stage.setTitle("老老老");
        stage.getIcons().add(new Image("sample/images/马.png"));
        stage.setScene(new Scene(root));
        stage.show();

    }

    @FXML
    protected void showdata() throws IOException {
        String filePath = fileText.getText();
        if(filePath==null || "".equals(filePath.trim())){
            Dialogutil.InformationDialog("提示框","尚未选择文件");
            return;
        }
        stage.hide();
        tableStage = new Stage();
        ImportExecl importExecl = new ImportExecl();
        List<List<String>> dataList = importExecl.read(filePath);
        Person person=null;
        for (List<String> rowdata : dataList) {
            if (rowdata.get(1) == null || "".equals(rowdata.get(1).trim())) {
                continue;
            }
            person=new Person();

            String[] fields= Reflectutil.getFieldNames(person);
            for(int i=0;i<fields.length;i++){
                Reflectutil.setValue(person,fields[i],rowdata.get(i));
            }


            pList.add(person);
        }
        Parent root = FXMLLoader.load(getClass().getResource("table.fxml"));
        TableView<Person> tableView = (TableView<Person>) root.lookup("#table");
        addCellColumns(tableView,new Person());

        tableView.setItems(pList);
        tableStage.setTitle("列表");
        tableStage.getIcons().add(new Image("sample/images/马.png"));
        tableStage.setScene(new Scene(root));
        tableStage.show();


    }

    @FXML
    public void doImport(){

        UpDialog.showUpload(pList);



    }

    @FXML
    protected void showchoose() {
        tableStage.hide();
        stage.show();
    }

    public void addCellColumns(TableView tableView,Object target) {

        List<TableColumn> tableColumnList=new ArrayList<>();
        String[] fields=Reflectutil.getFieldNames(target);
        for(int i=0;i<fields.length;i++){

            TableColumn tableColumn=new TableColumn(rightColumns[i]);
            tableColumn.setCellValueFactory(new PropertyValueFactory<>(fields[i]));
            tableColumn.setMinWidth(100);
            tableColumnList.add(tableColumn);


        }
        ObservableList<TableColumn> tableColumns=FXCollections.observableList(tableColumnList);
        tableView.getColumns().addAll(tableColumns);

    }
}
